package com.tiyende.credit.loop.sdk.analyse.util

import com.tiyende.credit.loop.request.HttpHelper
import com.tiyende.credit.loop.request.IHttpCallback
import com.tiyende.credit.loop.sdk.analyse.AnalyseBuilder
import com.tiyende.credit.loop.utils.log.JsonFormat
import com.tiyende.credit.loop.utils.log.TiyendeLogger
import org.json.JSONObject


object UrlHelper {

    fun combineUrl(): String {
        if (AnalyseBuilder.host.isBlank() or
            AnalyseBuilder.zone.isBlank() or
            AnalyseBuilder.pathCommon.isBlank()
        ) {
            return ""
        }

        val zone = AnalyseBuilder.zone
        AnalyseBuilder.zone = zone.replace("/", "")

        val pathCommon = AnalyseBuilder.pathCommon
        AnalyseBuilder.pathCommon = if (pathCommon.startsWith("/")) {
            pathCommon.replace("/", "")
        } else {
            pathCommon
        }

        return AnalyseBuilder.host + "/" + AnalyseBuilder.zone + "/" + AnalyseBuilder.pathCommon
    }
}

object AnalyseRequestUtil {
    fun doPostAction(toUrl: String, body: String) {
        if (toUrl.isBlank()) return
        if (body.isBlank()) return

        TiyendeLogger.i("Analyse Url：  $toUrl")
        TiyendeLogger.i("Analyse Params： $body")

        HttpHelper.post(toUrl, JSONObject(body),object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                Thread { TiyendeLogger.i("Analyse Response：${JsonFormat.format(data.toString())}") }.start()
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
//        OkHttp.post<String>(toUrl)
//            .tag(this)
//            .upJson(body)
//            .execute(object : StringCallback() {
//                override fun onSuccess(response: Response<String>) {
//                    val result = TiyendeAesUtil.decryptDefault(response.body())
//                    Thread { TiyendeLogger.i("Analyse Response：${JsonFormat.format(result)}") }.start()
//                }
//                override fun onError(response: Response<String>) {
//                }
//                override fun onFinish() {
//                }
//            })
    }
}


